package com.chuanyang.driverh.mainhome.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.response2.join.FleetDriverMsgListBean;
import com.chuanyang.driverh.response2.join.RcyRecommendDriverEntity;
import com.chuanyang.driverh.utils.widget.GlideCircleTransform;

import java.util.ArrayList;
import java.util.List;

/**
 * 相思无用，唯别而已。别期若有定，千般煎熬又何如，莫道黯然销魂，何处柳暗花明。
 * Created by sky on 2016/8/24.
 */
public class RcyMotorcadeDriverAdapter extends RecyclerView.Adapter<RcyMotorcadeDriverAdapter.ViewHolder>
implements View.OnClickListener{

    public List<FleetDriverMsgListBean> rcyEntityList = new ArrayList<>();
    public Context mContext;

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, RcyRecommendDriverEntity data);
    }
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public RcyMotorcadeDriverAdapter(List<FleetDriverMsgListBean> rcyList , Context context){
        this.rcyEntityList = rcyList;
        this.mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_motorcade_driver,parent,false);
        ViewHolder vh = new ViewHolder(view);
        view.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tv_driver_name.setText(rcyEntityList.get(position).getUsername());
        Glide.with(mContext).load(rcyEntityList.get(position).getUserImage()).crossFade().placeholder(R.mipmap.group_chat).
                error(R.mipmap.group_chat).transform(new GlideCircleTransform(mContext)).into(holder.iv_driver_img);
        holder.tv_driver_capacity.setText(rcyEntityList.get(position).getCapacity()+"t");
        holder.itemView.setTag(rcyEntityList.get(position));
    }


    @Override
    public int getItemCount() {
        return rcyEntityList.size();
    }

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(view, (RcyRecommendDriverEntity) view.getTag());
        }
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView iv_driver_img;
        private TextView tv_driver_name;
        private TextView tv_driver_capacity;
        public ViewHolder(View view){
            super(view);
            tv_driver_name = (TextView) view.findViewById(R.id.tv_driver_name);
            iv_driver_img = (ImageView) view.findViewById(R.id.iv_driver_img);
            tv_driver_capacity = (TextView) view.findViewById(R.id.tv_driver_capacity);
        }
    }
}
